<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=">
    <title>系统管理 - 航班乘客付费选座预测系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body class="bg-gray-100">
    <div class="container mx-auto px-4 py-8 max-w-6xl">
        <header class="text-center mb-8">
            <h1 class="text-3xl font-bold text-gray-800">航班乘客付费选座预测系统</h1>
            <p class="text-gray-600 mt-2">系统管理面板</p>
        </header>

        <!-- 导航菜单 -->
        <nav class="bg-white rounded-lg shadow-md p-4 mb-8">
            <ul class="flex flex-wrap gap-4 justify-center">
                <li><a href="/" class="text-blue-600 hover:text-blue-800 flex items-center"><i class="fa fa-home mr-1"></i>首页</a></li>
                <li><a href="/admin" class="text-purple-600 hover:text-purple-800 flex items-center border-b-2 border-purple-600"><i class="fa fa-cog mr-1"></i>管理面板</a></li>
                <li><a href="/logout" class="text-red-600 hover:text-red-800 flex items-center"><i class="fa fa-sign-out mr-1"></i>退出登录</a></li>
                <li class="ml-auto text-gray-600">管理员: {{ current_user.username }}</li>
            </ul>
        </nav>

        <!-- 信息卡片区 -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            <div class="bg-gradient-to-r from-blue-500 to-blue-700 rounded-lg shadow-md text-white p-6">
                <div class="flex justify-between items-center">
                    <div>
                        <p class="text-sm uppercase">总用户数</p>
                        <p class="text-3xl font-bold mt-1">{{ stats.total_users }}</p>
                    </div>
                    <i class="fa fa-users text-3xl opacity-80"></i>
                </div>
                <div class="mt-4">
                    <p class="text-sm">今日新用户: {{ stats.new_users_today }}</p>
                </div>
            </div>

            <div class="bg-gradient-to-r from-green-500 to-green-700 rounded-lg shadow-md text-white p-6">
                <div class="flex justify-between items-center">
                    <div>
                        <p class="text-sm uppercase">预测记录数</p>
                        <p class="text-3xl font-bold mt-1">{{ stats.total_predictions }}</p>
                    </div>
                    <i class="fa fa-bar-chart text-3xl opacity-80"></i>
                </极div>
                <div class="mt-4">
                    <p class="text-sm">今日预测次数: {{ stats.predictions_today }}</p>
                </div>
            </div>

            <div class="bg-gradient-to-r from-purple-500 to-purple-700 rounded-lg shadow-md text-white p-6">
                <div class="flex justify-between items-center">
                    <div>
                        <p class="text-sm uppercase">当前模型</p>
                        <p class="text-xl font-bold mt-1">{{ model_info.name }} v{{ model_info.version }}</p>
                    </div>
                    <i class="fa fa-cogs text-3xl opacity-80"></i>
                </div>
                <div class="mt-4">
                    <p class="text-sm">数据记录: {{ model_info.data_size }} | 准确率: {{ model_info.accuracy|round(2)*100 }}%</p>
                </div>
            </div>
        </div>

        <!-- 性能图表 -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">模型性能历史 <span class="text-gray-500 text-sm font-normal">(近30天)</span></h2>
            <div class="h-64">
                {{ performance_chart|safe }}
            </div>
        </div>

        <!-- 模型管理 -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">模型管理</h2>
            <div class="flex flex-wrap gap-4">
                <form action="/admin/train_model" method="post">
                    <button type="submit" class="bg-green-600 hover:bg-green-700 text-white px-6 py-3 rounded-md flex items-center">
                        <i class="fa fa-refresh mr-2"></i>立即重新训练模型
                    </button>
                </form>

                <form action="/admin/integrate_data">
                    <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-3 rounded-md flex items-center">
                        <i class="fa fa-database mr-2"></i>整合新数据
                    </button>
                </form>
            </div>

            <div class="mt-6">
                <h3 class="text-lg font-medium text-gray-700 mb-2">模型训练历史</h3>
                <div class="bg-gray-50 p-4 rounded-lg max-h-64 overflow-y-auto">
                    <table class="w-full text-sm text-left">
                        <thead>
                            <tr class="border-b">
                                <th class="py-2">时间</th>
                                <th class="py-2">模型</th>
                                <th class="py-2">准确率</th>
                                <th class="py-2">训练样本</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in training_logs %}
                            <tr class="border-b hover:bg-gray-100">
                                <td class="py-3">{{ log.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td class="py-3">{{ log.model_name }} v{{ log.version }}</td>
                                <td class="py-3">{{ (log.accuracy * 100)|round(2) }}%</td>
                                <td class="py-3">{{ log.training_size }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- 用户行为分析 -->
        <div class="bg-white rounded-lg shadow-md p-6">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">用户活跃度分析</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="text-lg font-medium text-gray-700 mb-3">活跃用户排行</h3>
                    <div class="space-y-3">
                        {% for user in active_users %}
                        <div class="flex items-center justify-between">
                            <div class="flex items-center">
                                <div class="w-8 h-8 rounded-full bg-blue-500 text-white flex items-center justify-center">
                                    <span class="text-xs">{{ user.username[0].upper() }}</span>
                                </div>
                                <div class="ml-3">
                                    <p class="text-gray-800 font-medium">{{ user.username }}{% if user.is_admin %} <span class="text-xs bg-purple-100 text-purple-800 px-2 py-1 rounded-full">管理员</span>{% endif %}</p>
                                    <p class="text-xs text-gray-500">注册于 {{ user.created_at.strftime('%Y-%m-%d') }}</p>
                                </div>
                            </div>
                            <div class="text-right">
                                <p class="font-bold">{{ user.prediction_count }}</p>
                                <p class="text-xs text-gray-500">预测次数</p>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>

                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="text-lg font-medium text-gray-700 mb-3">用户行为统计</h3>
                    <div class="grid grid-cols-2 gap-4">
                        <div class="bg-white p-4 rounded-lg shadow-sm border">
                            <p class="text-3xl font-bold text-center">{{ stats.avg_prediction_count|round(1) }}</p>
                            <p class="text-sm text-center text-gray-600">人均预测次数</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg shadow-sm border">
                            <p class="text-3xl font-bold text-center">{{ stats.active_users }}</p>
                            <p class="text-sm text-center text-gray-600">活跃用户(近7天)</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg shadow-sm border">
                            <p class="text-3xl font-bold text-center">{{ stats.admin_users }}</p>
                            <p class="text-sm text-center text-gray-600">管理员用户</p>
                        </div>
                        <div class="bg-white p4 rounded-lg shadow-sm border">
                            <p class="text-3xl font-bold text-center">{{ stats.new_users_7days }}</p>
                            <p class="text-sm text-center text-gray-600">7日内新用户</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <footer class="mt-12 text-center text-gray-500 text-sm">
            <p>航班乘客付费选座预测系统 &copy; 2023 | 管理员面板</p>
        </footer>
    </div>
</body>
</html>